

package com.yz.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yz.common.entity.*;
import com.yz.dao.DiscoverDao;
import com.yz.dao.SectionDao;
import com.yz.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("sectionService")
public class SectionServiceImpl extends ServiceImpl<SectionDao,SectionEntity> implements SectionService {
    @Autowired
    CheckService checkService;
    @Autowired
    SectionDetailService sectionDetailService;
    @Autowired
    ProjectService projectService;
    @Autowired
    CameraService cameraService;
    @Autowired
    DrawingService drawingService;

    @Override
    public long selectId() {
        return this.baseMapper.selectId();
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void postSection(SectionForm t,UserEntity user) {
        SectionEntity data = t.getSectionEntity();
        CheckEntity check = new CheckEntity();
        check.setCreateAt(data.getUpdateAt());
        check.setCreateUser(data.getCreateUser());
        check.setName(projectService.selectById(data.getProjectId()).getName());
        check.setNameForm(contant.type_name_form[2]);
        check.setProjectId(data.getProjectId());
        check.setStatus(1);
        check.setType(5);
        check.setTypeForm(3);
        long id = 0;
        if(data.getId()==0){
            this.baseMapper.insert(data);
            id = this.baseMapper.selectId();
            check.setDataId(id);
            check.setNumber(contant.type_all_form[4]+contant.type_detail_form[2]+id);
            checkService.insert(check);
        }else{
            id = data.getId();
            check.setDataId(id);
            check.setNumber(contant.type_all_form[4]+contant.type_detail_form[2]+id);
            checkService.update(check,new EntityWrapper<CheckEntity>().eq("number",contant.type_all_form[4]+contant.type_detail_form[2]+id));
            this.baseMapper.update(data,new EntityWrapper<SectionEntity>().eq("id",id));
            sectionDetailService.delete(new EntityWrapper<SectionDetailEntity>().eq("section_id",id));
        }
        List<SectionDetailEntity> collections = t.getSectionDetailEntities();
        for (int i = 0; i < collections.size(); i++) {
            SectionDetailEntity item = collections.get(i);
            item.setSectionId(id);
            sectionDetailService.insert(item);
        }
        //图片放入数据库
        if(t.getImageNumber()!=null&& t.getImageNumber().size()>0){
            cameraService.delete(new EntityWrapper<CameraEntity>().eq("data_id",id).eq("type_form",3));
            for (int i = 0; i <t.getImageNumber().size() ; i++) {
                CameraEntity item1 = t.getImageNumber().get(i);
                item1.setPosterId(user.getUserId());
                item1.setPostTime(data.getCreateAt());
                item1.setCreateUser(user.getUsername());
                item1.setProjectId(data.getProjectId());
                item1.setTypeWhere(1);
                item1.setTypeForm(3);
                item1.setDataId(id);
                item1.setFiledName("0");
                item1.setNumber(contant.type_all_form[4]+contant.type_detail_form[2]+id);
                cameraService.insert(item1);
            }
        }
        if(t.getDrawingNumber()!=null&& t.getDrawingNumber().size()>0){
            drawingService.delete(new EntityWrapper<DrawingEntity>().eq("data_id",id).eq("type_form",3));
            for (int i = 0; i <t.getDrawingNumber().size() ; i++) {
                DrawingEntity item1 = t.getDrawingNumber().get(i);
                item1.setPosterId(user.getUserId());
                item1.setPostTime(data.getCreateAt());
                item1.setCreateUser(user.getUsername());
                item1.setProjectId(data.getProjectId());
                item1.setTypeWhere(1);
                item1.setTypeForm(3);
                item1.setDataId(id);
                item1.setNumber(contant.type_all_form[4]+contant.type_detail_form[2]+id);
                drawingService.insert(item1);
            }
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public SectionForm getSectionDetail(long id) {
        SectionForm data = new SectionForm();
    SectionEntity t = new SectionEntity();
    t.setId(id);
    t = this.baseMapper.selectById(t);
        List<SectionDetailEntity> collections = sectionDetailService.selectList(new EntityWrapper<SectionDetailEntity>().eq("section_id",id));
        data.setSectionEntity(t);
        data.setSectionDetailEntities(collections);
        StringBuffer cc1 = new StringBuffer();
        List<CameraEntity> cc1s = cameraService.selectList(new EntityWrapper<CameraEntity>().eq("filed_name","0").eq("data_id",id).eq("type_form",3));
        List<DrawingEntity> dds = drawingService.selectList(new EntityWrapper<DrawingEntity>().eq("data_id",id).eq("type_form",3));
data.setDrawingNumber(dds);
        data.setImageNumber(cc1s);
        return data;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deleteSection(long id) {
        this.deleteById(id);
        cameraService.delete(new EntityWrapper<CameraEntity>().eq("data_id",id).eq("type_form",3));
        drawingService.delete(new EntityWrapper<DrawingEntity>().eq("data_id",id).eq("type_form",3));
        checkService.deleteMe(5,3,id);
        sectionDetailService.delete(new EntityWrapper<SectionDetailEntity>().eq("section_id",id));
    }
}
